package com.coocaa.ops.admin.v1.timedtask.provider.impl;

import com.coocaa.ops.admin.v1.timedtask.biz.TimedTaskBizImpl;
import com.coocaa.ops.admin.v1.timedtask.constant.TimedTaskTypeConstants;
import com.coocaa.ops.admin.v1.timedtask.provider.ITimedTaskProvider;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/6/16.
 * @description
 */
@Service
public class TimedTaskProviderImpl implements ITimedTaskProvider {
    @Autowired
    private TimedTaskBizImpl timedTaskBiz;

    public TimedTaskBizImpl getTimedTaskBiz() {
        return timedTaskBiz;
    }

    /**
     * 获取定时信息-定时上架
     *
     * @param resourceType
     * @param resourceId
     * @param resourceVersion
     * @return
     */
    @Override
    public TimedTaskInfoEntity queryTimedTaskForTimelyShelves(String resourceType, Integer resourceId, Integer resourceVersion) {
        QueryTemplate<TimedTaskInfoEntity> wrapper = new QueryTemplate<>();
        wrapper.eq("timed_type", TimedTaskTypeConstants.TIMELY_SHELVING)
                .eq("resource_id", resourceId)
                .eq("resource_type", resourceType)
                .eq("resource_version", resourceVersion)
        ;
        List<TimedTaskInfoEntity> list = timedTaskBiz.getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

    @Override
    public TimedTaskInfoEntity queryTimedTaskForRecommendPoolUpdate(Integer resourceId, String resourceType) {
        QueryTemplate<TimedTaskInfoEntity> wrapper = new QueryTemplate<>();
        wrapper.eq("timed_type", TimedTaskTypeConstants.RECOMMEND_POOL_UPDATE)
                .eq("resource_id", resourceId)
                .eq("resource_type", resourceType)
        ;
        List<TimedTaskInfoEntity> list = timedTaskBiz.getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

    /**
     * 定时数量限制
     *
     * @return
     */
    @Override
    public int queryLimitTaskNum() {
        QueryTemplate wrapper = new QueryTemplate();
        wrapper.in("status", new Integer[]{7, 3});
        return timedTaskBiz.getMapper().selectCount(wrapper);
    }

    /**
     * 保存信息
     *
     * @param entity
     */
    @Override
    public void save(TimedTaskInfoEntity entity) {
        timedTaskBiz.getMapper().insert(entity);
    }

    /**
     * 更新信息-根据ID
     *
     * @param entity
     */
    @Override
    public void updateById(TimedTaskInfoEntity entity) {
        entity.setLastUpdateDate(new Date());
        timedTaskBiz.getMapper().updateById(entity);
    }

    /**
     * 更新定时任务状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateTimedTaskStatusById(Integer id, Integer status) {
        TimedTaskInfoEntity updateTimedTask = new TimedTaskInfoEntity();
        updateTimedTask.setId(id);
        updateTimedTask.setStatus(status);
        timedTaskBiz.getMapper().updateById(updateTimedTask);
    }

    /**
     * 删除-根据ID
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        timedTaskBiz.getMapper().deleteById(id);
    }


}
